<?php
class ProductController extends Controller {
	public $layout='//layouts/column2';
	public function actionList($id) {
		$criteria = new CDbCriteria ();
		$criteria->limit = 10;
		$criteria->with = array (
				'cover' 
		);
		$criteria->compare ( 'category_id', $id );
		$proList = Product::model ()->findAll ( $criteria );
		Yii::app ()->clientScript->registerScript ( 'category_list', "var category_id={$id};", CClientScript::POS_HEAD );
		$this->render ( 'list', array (
				'id' => $id,
				'proList' => $proList 
		) );
	}
	public function actionSearchList($productName='') {
		$criteria = new CDbCriteria ();
		$criteria->limit = 10;
		$criteria->addSearchCondition('name', $productName);
		$criteria->with = array (
				'cover'
		);
		$proList = Product::model ()->findAll ( $criteria );
		$this->render ( 'list', array (
				'proList' => $proList
		) );
	}
	public function actionService() {
		$id = Yii::app ()->request->getParam ( 'id' );
		$criteria = new CDbCriteria ();
		$criteria->limit = 10;
		$criteria->with = array (
				'cover' 
		);
		$criteria->compare ( 'category_id', $id );
		$proList = Product::model ()->findAll ( $criteria );
		$res = array ();
		foreach ( $proList as $pro ) {
			$resItem = array ();
			$resItem ['id'] = $pro->id;
			$resItem ['name'] = $pro->name;
			$resItem ['price'] = $pro->price;
			$resItem ['currency'] = $pro->currency;
			$resItem ['unit']	= $pro->currency=='USD'?'$':'￥';
			$resItem ['cover'] = $pro->cover ? $pro->cover->realUrl : '';
			$res [] = $resItem;
		}
		Utils::ajaxReturn ( $res );
	}
	
	
	public function actionDetail($id){
		$product = Product::model()->with('cover')->findByPk($id);
		$this->render('detail',array('product'=>$product));
	}
	
	public function actionSearch($term = ''){
		$criteria = new CDbCriteria();
		$criteria->limit =5;
		$criteria->select = 'name';
		$criteria->addSearchCondition('name', $term);
		$productList = Product::model()->findAll($criteria);
		$proArr = array();
		foreach($productList as $pl){
			$proArr[] = $pl->name;
		}
		echo CJSON::encode($proArr);
		Yii::app()->end();
	}
}

?>